<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="renderer" content="webkit|ie-comp|ie-stand" />
<meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE">
<title>课程-答疑-首页</title>

<%@ include file="/common/commonLib.jsp"%>
<link rel="stylesheet" type="text/css" href="<c:url value='/css/forum-index.css'/>" />
<link rel="stylesheet" type="text/css" href="<c:url value='/wangEditor/dist/css/wangEditor.css'/>" />

<script src="<c:url value='/js/forum-index.js'/>" type="text/javascript" charset="utf-8"></script>

</head>
<body>
	<div class="container-fluid">
		<div class="row">
			<div class="col-md-12">
				<%@include file="/common/header.jsp"%>
			</div>
			<div class="col-md-12">
				<%@include file="common/courseHeader.jsp"%>
			</div>
			<div class="col-md-3">
				<%@include file="common/courseLeft.jsp"%>
			</div>
			<div class="col-md-9">
				<%@include file="common/courseNavbar.jsp"%>
			</div>
			<div class="col-md-9">
				<!-- 课程答疑首页 -->
				<div class="forum-index">					
					<div class="row">
						<div class="col-md-8"></div>
						<div class="col-md-2" style="text-align: right;">
							<!-- TODO: -->
							<input style="display:none;"type="button" id="manage-module" class="btn btn-primary" value="管理版块">
						</div>
						<div class="col-md-2" style="text-align: right;">
							<input type="button" id="creat-new-module-btn" class="btn btn-primary" value="创建版块">
						</div>
					</div>
					<div class="row">
						<!--版块列表 -->
						<div class="col-md-12">
							<table class="table table-hover">
								<thead>
									<tr>
										<th class="col1">版块名</th>
										<th class="col2">描述</th>
										<th class="col3">帖子</th>
										<th class="col4">回复</th>
										<th class="col5">创建者</th>
										<th class="col6">创建时间</th>
									</tr>
								</thead>
								<tbody id="module-list">
									<tr>
										<td>学习心得</td>
										<td>交换学习心得</td>
										<td>5</td>
										<td>55</td>
										<td>王慧朝</td>
										<td>20180117</td>
									</tr>
									<tr>
										<td>日常交流</td>
										<td>日常生活交流</td>
										<td>5</td>
										<td>55</td>
										<td>王慧朝</td>
										<td>20180117</td>
									</tr>
									<tr>
										<td>意见反馈</td>
										<td>反馈你的意见</td>
										<td>5</td>
										<td>55</td>
										<td>王慧朝</td>
										<td>20180117</td>
									</tr>
								</tbody>
							</table>
							<!-- 分页 -->
							<div class="page-content">
								<nav class="pagination" role="navigation">
									<a class="last-page"> <i class="fa fa-angle-left"></i>
									</a> <span class="page-number">第1页/共7页</span> <a class="next-page">
										<i class="fa fa-angle-right"></i>
									</a>
								</nav>
							</div>

						</div>
					</div>
				</div>
				<!-- 首页 end -->
			</div>

		</div>
	</div>


	<!-- 版块创建 -->
	<div class="row" id="create-new-module-layer" style="display: none;">
		<div class="col-md-1"></div>
		<div class="col-md-10 course-forum-creat">
			<div class="row">
				<div class="col-md-12">
					<!-- 版块名称 -->
					<label for="">版块名称</label> <input type="text" class="form-control" name="module-name">
				</div>
			</div>

			<div class="row">
				<div class="col-md-12">
					<label for="">版块描述</label>
				</div>
				<div class="col-md-12">
					<div id="editor-task"></div>
				</div>
			</div>
			<div class="row" style="margin-top: 2rem; margin-bottom: 2rem; text-align: center;">
				<input type="button" id="module-creat-btn" class="btn btn-success" value="创建版块">
			</div>
		</div>
		<div class="col-md-1"></div>
	</div>
	
	<!-- 版块管理 -->
	<div class="row" id="module-manage-layer" style="display: none;">
		<div class="col-md-1"></div>
		<div class="col-md-10">
			<div class="row">
				<div class="col-md-12">
					<table class="table table-hover">
						<thead>
							<tr>
								<td>名称</td>
								<td>创建者</td>
								<td>操作</td>
							</tr>
						</thead>
						<tbody id="module-manage-list">
							<tr>
								<td>版块名称</td>
								<td>创建者</td>
								<td><input type="radio" /></td>
							</tr>
							<tr>
								<td>版块名称</td>
								<td>创建者</td>
								<td>checkbox</td>
							</tr>
						</tbody>
					</table>
				</div>
				<div class="col-md-12">
					<p><input class="radio" name="" value="NO" />删除所有数据</p>
					<p><input class="radio" name="" value="NO" />隐藏版块</p>
				</div>
				<div class="col-md-12">
					<input type="button" id="module-manage-btn" class="btn btn-success" value="确定">
				</div>
			</div>
		</div>
		<div class="col-md-1"></div>
	</div>
	
	<%@include file="/common/footer.jsp"%>

	<script src="<c:url value='/wangEditor/dist/js/wangEditor.js'/>" type="text/javascript" charset="utf-8"></script>
</body>
<script>
	var data=${data};
	$(document).ready(function() {
		if (data.status != "success") {
			alert(data.message);
			return;
		}
		
		init();
		
		/* fetchCourse(data.data.courseId);
		initTable(data.data);
		changeUrl(data.data.search.courseId); */
	})

	var initTable = function(data){
		var moduleList=$("#module-list");
		moduleList.empty();
		for(var i=0;i<data.list.length;i++){
			var item=data.list[i];			
			var module = "";
			module += ("<tr onclick=\"window.location.href='<c:url value='/module/detail?id="+item.id+"'/>'\">")
			module += ("<td>"+item.name+"</td>");
			module += ("<td>"+item.description+"</td>");
			module += ("<td>"+item.postCount+"</td>");
			module += ("<td>"+item.replyCount+"</td>");
			module += ("<td>"+item.createName+"</td>");
			module += ("<td>"+item.createDate+"</td>");
			module += ("</tr>");
			
			moduleList.append(module);
		};
		initPages(data.pageNo,data.allpages);
	}
	var search = function(){
		var taskType=$("#searchTaskType  option:selected").val();
		var isQuote =$("#searchIsQuote  option:selected").val();
		var json={
				courseId:data.data.courseId,
				type:taskType,
				isQuote:isQuote,
		};
		if(isQuote=="ALL"){
			delete json.isQuote;
		}
		if(taskType=="ALL"){
			delete json.type;
		}
		taskList(json);
	}

	var initList= function(){
		var json={
				courseId:data.data.courseId	
		};
		taskList(json);
	}

	var taskList = function(json){
		$.get(CONTEXT_PATH+"task/ajaxListData", {
			data : JSON.stringify(json)
		}, function(data) {
			if(data.status=="success"){
				initTable(data.data);
			}else{
				alert(data.message);
			}
		});
	}

	var addTask= function (){
		var courseId=data.data.courseId;
		var type=$("#addTaskType  option:selected").val();
		var title =$("input[name='title']").val();
		var description=$("textarea[id='description']").val();
		var requirement=$("textarea[id='requirement']").val();
		var standardAnswer=$("textarea[id='standardAnswer']").val();
		
		var attachName=$("#task-file-list input[name='attachName']");
		var attachKey=$("#task-file-list input[name='attachKey']");
		var attachFiles=[];
		
		for(var i=0;i<attachName.length;i++){
			var item={
					name:attachName[i].value,
					url:attachKey[i].value
					};
			attachFiles.push(item);
		}
		
		var answerAttachName=$("#answer-file-list input[name='answerAttachName']");
		var  answerAttachKey=$("#answer-file-list input[name='answerAttachKey']");
		var  answerAttachFiles=[];
		
		for(var i=0;i< answerAttachName.length;i++){
			var item={
					name: answerAttachName[i].value,
					url: answerAttachKey[i].value
					};
			 answerAttachFiles.push(item);
		}
		
		
		var json={
				courseId:courseId,
				type:type,
				title:title,
				description:description,
				requirement:requirement,
				standardAnswer:standardAnswer,
				attachment:{
					files:attachFiles
				},
				answerAttachment:{
					files:answerAttachFiles
				},
		};
		TaskCreate(json);
	}

	var TaskCreate = function(json){
		$.post(CONTEXT_PATH+"task/ajaxCreate", {
			data : JSON.stringify(json)
		}, function(data) {
			if(data.status=="success"){
				window.location.reload();
			}else{
				alert(data.message);
			}
		});
	}
	//渲染界面
	 var initPages = function(pageNo,allpages){
		
		 var lastPageObj=$(".last-page");
			var nextPageObj=$(".next-page");
			var pageNumberObj=$(".page-number");
			var lastPage=pageNo-1;
			if(lastPage<=0){//设置disableed
				lastPage=1;
				//lastPageObj.removeAttr("onclick");
			}else{
				 $(".last-page").bind("click",{pageNo:lastPage},getPage);
			};
			var nextPage = pageNo+1;
			if(nextPage>allpages){
				nextPage=allpages;
				//nextPageObj.removeAttr("onclick");
			}else{
				 $(".next-page").bind("click",{pageNo:nextPage}, getPage);
				//$(".next-page").click({pageNo:nextPage},getPage());
			}
			pageNumberObj.text("第"+pageNo+"页/共"+allpages+"页");
	 }

	 var getPage = function(event){
		 //alert(event.data.pageNo);
	 	var taskType=$("#searchTaskType  option:selected").val();
	 	var isQuote =$("#searchIsQuote  option:selected").val();
	 	var json={
	 			courseId:data.data.courseId,
	 			type:taskType,
	 			isQuote:isQuote,
	 	};
	 	if(isQuote=="ALL"){
	 		delete json.isQuote;
	 	}
	 	if(taskType=="ALL"){
	 		delete json.type;
	 	}
	 	json.pageNo=event.data.pageNo;
	 	taskList(json);
	 }
</script>
</html>